package com.base.cn.platform.os.controller.user.member;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.service.manage.user.member.CusUserMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户会员记录管理
 *
 * @author ll
 * @create 2018-04-26
 */
@Controller
@RequestMapping("/admin/user/cus")
public class AdminUserMemberController extends AdminBaseController {

    @Autowired
    private CusUserMemberService cusUserMemberService;

    /**
     * 去添加修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/toSaveUserMemberPage")
    public String toSaveUserMemberPage(Model model,
                                       @RequestParam(value = "id",required = false)BigDecimal id){
        Map<String,Object> member=new HashMap<>();
        if(DataUtil.idIsNotNull(id)){
            member = cusUserMemberService.queryUserMemberById(id);
        }
        model.addAttribute("member",member);
        return resultPage("/user/member/member_create");
    }

    /**
     * 去添加修改页面
     * @param member
     * @return
     */
    @RequestMapping("/saveUpdateUserMember")
    @ResponseBody
    public Map<String,Object> saveUpdateUserMember(@RequestBody Map<String,Object> member){
        return cusUserMemberService.saveUpdateUserMember(member);
    }

    /**
     * 去列表页面
     * @return
     */
    @RequestMapping("/toUserMemberListPage")
    public String toUserMemberListPage(){
        return resultPage("/user/member/member_list");
    }


    /**
     * 去列表页面
     * @return
     */
    @RequestMapping("/queryUserMemberListPage")
    public String queryUserMemberListPage(Model model,
                                          @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Map<String,Object> dataMap =cusUserMemberService.queryUserMemberListPage(currentPage);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/member/member_ajax_list");
    }

    /**
     * 修改状态
     * @return
     */
    @RequestMapping("/updateUserMemberStatus")
    @ResponseBody
    public Map<String,Object> updateUserMemberStatus(@RequestParam("id")BigDecimal id,
                                                      @RequestParam("status")Integer status){
      return cusUserMemberService.updateUserMemberStatus(id,status);
    }


    /**
     * 去选择会员列表页面
     * @return
     */
    @RequestMapping("/selectMemberList")
    public String selectMemberList(Model model,
                                   @RequestParam(value = "selectType",required = false,defaultValue = "checkbox") String selectType){
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        params.put("memberType",1);
        List<Map<String,Object>> memberList =cusUserMemberService.queryUserMemberList(params);
        model.addAttribute("memberList",memberList);
        model.addAttribute("selectType",selectType);
        return resultPage("/user/member/select_member_list");
    }

}
